home *** CD-ROM | disk | FTP | other *** search
- /***
- *
- * Utils.c - String and memory utility functions
- *
- * Copyright (c) 1991, 1994 Symantec Corporation. All rights reserved.
- * Edited by Christopher E. Hyde, 1995
- *
- ***/
-
-
- /*
- * ctype.c
- *
- * Copyright (c) 1991 Symantec Corporation. All rights reserved.
- *
- */
-
- #include <ctype.h>
-
-
- int
- toupper(int c)
- {
- return(islower(c) ? (c ^ 0x20) : c);
- }
-
-
- int
- tolower(int c)
- {
- return(isupper(c) ? (c ^ 0x20) : c);
- }
-
-
- #define __UPPX (__XDIG|__UPPR)
- #define __LOWX (__XDIG|__LOWR)
-
- char __ctype[256] = {
- __CNTL,__CNTL,__CNTL,__CNTL,__CNTL,__CNTL,__CNTL,__CNTL,
- __CNTL,__WHIT,__WHIT,__WHIT,__WHIT,__WHIT,__CNTL,__CNTL,
- __CNTL,__CNTL,__CNTL,__CNTL,__CNTL,__CNTL,__CNTL,__CNTL,
- __CNTL,__CNTL,__CNTL,__CNTL,__CNTL,__CNTL,__CNTL,__CNTL,
- __SPAC,__PUNC,__PUNC,__PUNC,__PUNC,__PUNC,__PUNC,__PUNC,
- __PUNC,__PUNC,__PUNC,__PUNC,__PUNC,__PUNC,__PUNC,__PUNC,
- __DIGT,__DIGT,__DIGT,__DIGT,__DIGT,__DIGT,__DIGT,__DIGT,
- __DIGT,__DIGT,__PUNC,__PUNC,__PUNC,__PUNC,__PUNC,__PUNC,
- __PUNC,__UPPX,__UPPX,__UPPX,__UPPX,__UPPX,__UPPX,__UPPR,
- __UPPR,__UPPR,__UPPR,__UPPR,__UPPR,__UPPR,__UPPR,__UPPR,
- __UPPR,__UPPR,__UPPR,__UPPR,__UPPR,__UPPR,__UPPR,__UPPR,
- __UPPR,__UPPR,__UPPR,__PUNC,__PUNC,__PUNC,__PUNC,__PUNC,
- __PUNC,__LOWX,__LOWX,__LOWX,__LOWX,__LOWX,__LOWX,__LOWR,
- __LOWR,__LOWR,__LOWR,__LOWR,__LOWR,__LOWR,__LOWR,__LOWR,
- __LOWR,__LOWR,__LOWR,__LOWR,__LOWR,__LOWR,__LOWR,__LOWR,
- __LOWR,__LOWR,__LOWR,__PUNC,__PUNC,__PUNC,__PUNC,__CNTL,
- };
-
-
- /*
- * str.c
- *
- * Copyright (c) 1991 Symantec Corporation. All rights reserved.
- *
- */
-
- #pragma options(!require_protos)
- #if !__powerc
- #include <size_t.h>
- #else
- #include "string.h"
- #endif
-
- #if !__powerc
- char *
- strcpy(/* char *s1, const char *s2 */)
- {
- asm {
- movea.l 4(sp),a0 ; A0 = s1
- movea.l 8(sp),a1 ; A1 = s2
- move.l a0,d0 ; D0.L = result
- @1 move.b (a1)+,(a0)+
- bne.s @1
- }
- }
- #else
- char *
- strcpy(char *s1, const char *s2)
- {
- char *ret = s1;
-
- while ((*s1++ = *s2++) != 0)
- ;
-
- return ret;
- }
- #endif
-
-
- #if !__powerc
- int
- strcmp(/* const char *s1, const char *s2 */)
- {
- asm {
- movea.l 4(sp),a0 ; A0 = s1
- movea.l 8(sp),a1 ; A1 = s2
- moveq #0,d0 ; D0.L = result
- bra.s @2
- @1 tst.b d1
- beq.s @4
- @2 move.b (a0)+,d1
- cmp.b (a1)+,d1
- beq.s @1
- bhi.s @3
- subq.l #2,d0
- @3 addq.l #1,d0
- @4 }
- }
- #else
- int
- strcmp(const char *s1, const char *s2)
- {
- char c1, c2, dif;
-
- for (;;) {
- if (!(c1 = *s1++))
- return *s2 ? -1 : 0;
- if (!(c2 = *s2++))
- return 1;
- if (!(dif = (c1 - c2)))
- continue;
- if (dif < 0)
- return -1;
- else
- return 1;
- }
-
- return 0;
- }
- #endif
-
-
- #if !__powerc
- char *
- strchr(/* const char *s, int c */)
- {
- asm {
- movea.l 4(sp),a0 ; A0 = s
- #if __option(int_4)
- move.b 11(sp),d1 ; D1.B = (char) c
- #else
- move.b 9(sp),d1 ; D1.B = (char) c
- #endif
- moveq #0,d0 ; D0.L = result
- bra.s @2
- @1 tst.b (a0)+
- beq.s @3
- @2 cmp.b (a0),d1
- bne.s @1
- move.l a0,d0
- @3 }
- }
- #else
- char *
- strchr(const char *s, int c)
- {
- do {
- if (*s == c)
- return (char *)s;
- } while (*s++);
-
- return 0;
- }
- #endif
-
-
- #if !__powerc
- char *
- strpbrk(/* const char *s1, const char *s2 */)
- {
- asm {
- movea.l 4(sp),a0 ; A0 = s1
- moveq #0,d0 ; D0.L = result
- @1 move.b (a0)+,d1
- beq.s @3
- movea.l 8(sp),a1 ; A1 = s2
- @2 move.b (a1)+,d2
- beq.s @1
- cmp.b d1,d2
- bne.s @2
- subq.l #1,a0
- move.l a0,d0
- @3 }
- }
- #else
- char *
- strpbrk(const char *s1, const char *s2)
- {
- const char *t;
- char c;
-
- while ((c = *s1++) != 0) {
- for (t = s2; *t; ++t)
- if (c == *t)
- return (char *)(s1 - 1);
- }
-
- return 0;
- }
- #endif
-
-
- #if !__powerc
- char *
- strrchr(/* const char *s, int c */)
- {
- asm {
- movea.l 4(sp),a0 ; A0 = s
- #if __option(int_4)
- move.b 11(sp),d1 ; D1.B = (char) c
- #else
- move.b 9(sp),d1 ; D1.B = (char) c
- #endif
- moveq #0,d0 ; D0.L = result
- @1 cmp.b (a0),d1
- bne.s @2
- move.l a0,d0
- @2 tst.b (a0)+
- bne.s @1
- }
- }
- #else
- char *
- strrchr(const char *s, int c)
- {
- const char *res = NULL;
-
- do {
- if (*s == c)
- res = s;
- } while (*s++);
-
- return (char *)res;
- }
- #endif
-
-
- #if !__powerc
- size_t
- strlen(/* const char *s */)
- {
- asm {
- moveq #-1,d0 ; D0.L = result
- movea.l 4(sp),a0 ; A0 = s
- @1 addq.l #1,d0
- tst.b (a0)+
- bne.s @1
- }
- }
- #else
- size_t
- strlen(const char *s)
- {
- size_t len = 0;
-
- while (*s++)
- ++len;
-
- return len;
- }
- #endif
-
-
- /*
- * strn.c
- *
- * Copyright (c) 1994 Symantec Corporation. All rights reserved.
- *
- */
-
- #if !__powerc
- #pragma options(!require_protos)
- #else
- #include "string.h"
- #endif
-
-
- #if !__powerc
- char *
- strncpy(/* char *s1, const char *s2, size_t n */)
- {
- asm {
- move.l 4(sp),d0 ; D0.L = result
- movea.l d0,a0 ; A0 = s1
- movea.l 8(sp),a1 ; A1 = s2
- move.l 12(sp),d1 ; D1.L = n
- beq.s @3
- @1 move.b (a1),(a0)+
- beq.s @2
- addq.l #1,a1
- @2 subq.l #1,d1
- bne.s @1
- @3 }
- }
- #else
- char *
- strncpy(char *s1, const char *s2, size_t n)
- {
- char *res = s1;
-
- while (n--) {
- if ((*s1++ = *s2)!=0)
- ++s2;
- }
- return res;
- }
- #endif
-
-
- #if !__powerc
- int
- strncmp(/* const char *s1, const char *s2, size_t n */)
- {
- asm {
- moveq #0,d0 ; D0.L = result
- movea.l 4(sp),a0 ; A0 = s1
- movea.l 8(sp),a1 ; A1 = s2
- move.l 12(sp),d1 ; D1.L = n
- bra.s @2
- @1 tst.b d2
- beq.s @4
- subq.l #1,d1
- @2 beq.s @4
- move.b (a0)+,d2
- cmp.b (a1)+,d2
- beq.s @1
- bhi.s @3
- subq.l #2,d0
- @3 addq.l #1,d0
- @4 }
- }
- #else
- int
- strncmp(const char *s1, const char *s2, size_t n)
- {
- char c1, c2, dif;
-
- while (n--) {
- if (!(c1 = *s1++))
- return *s2 ? -11 : 0;
- if (!(c2 = *s2++))
- return 1;
- if (!(dif = (c1 - c2)))
- continue;
- if (dif < 0)
- return -1;
- else
- return 1;
- }
- return 0;
- }
- #endif
-
-
- /*
- * mem.c
- *
- * Copyright (c) 1991 Symantec Corporation. All rights reserved.
- *
- */
-
- #pragma options(!require_protos)
-
-
- #include <size_t.h>
-
- #if !__powerc
- void *
- memcpy(/* void *s1, const void *s2, size_t n */)
- {
- asm {
- move.l 4(sp),d0 ; D0.L = result
- movea.l d0,a0 ; A0 = s1
- movea.l 8(sp),a1 ; A1 = s2
- move.l 12(sp),d1 ; D1.L = n
- bra.s @2
- @1 move.b (a1)+,(a0)+
- subq.l #1,d1
- @2 bne.s @1
- }
- }
- #else
- void *
- memcpy(void *s1, const void *s2, size_t n)
- {
- const char *from = (char *)s2;
- char *to = (char *)s1;
-
- while (n--) {
- *to++ = *from++;
- }
-
- return s1;
- }
- #endif
-
-
- #if !__powerc
- void *
- memchr(/* const void *s, int c, size_t n */)
- {
- asm {
- moveq #1,d0 ; D0.L = result
- movea.l 4(sp),a0 ; A0 = s
- #if __option(int_4)
- move.b 11(sp),d1 ; D1.B = (unsigned char) c
- move.l 12(sp),d2 ; D2.L = n
- #else
- move.b 9(sp),d1 ; D1.B = (unsigned char) c
- move.l 10(sp),d2 ; D2.L = n
- #endif
- bra.s @2
- @1 subq.l #1,d2
- @2 beq.s @3
- cmp.b (a0)+,d1
- bne.s @1
- move.l a0,d0
- @3 subq.l #1,d0
- }
- }
- #else
- void *
- memchr(const void *_s, int c, size_t n)
- {
- const char *s = (const char *)_s;
- char *res = (char *)1;
-
- while (n--)
- if (*s++ == c)
- return (void *)(s - 1);
-
- return 0;
- }
- #endif
-